package com.ophiux.cs.application.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.AppResultCode;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.DeleteUserPatientRequest;
import com.ophiux.cs.application.user.dto.DeleteUserPatientResponse;
import com.ophiux.cs.application.user.service.IDeleteUserPatientAppService;
import com.ophiux.cs.domain.core.DomainResult;
import com.ophiux.cs.domain.user.model.aggregate.UserPatientInfo;
import com.ophiux.cs.domain.user.model.valueobj.PatientsValueObj;
import com.ophiux.cs.infrastructure.repositories.user.PatientRepository;
import com.ophiux.utils.common.ShiftUtils;
import com.ophiux.utils.common.StringUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class DeleteUserPatientAppService implements IDeleteUserPatientAppService {
	@Autowired
	PatientRepository patientRepository;
	
	@SuppressWarnings("rawtypes")
	@Override
	public Result<DeleteUserPatientResponse> deleteUserPatient(DeleteUserPatientRequest dto) {
//		DeleteUserPatientResponse resp = new DeleteUserPatientResponse();
		try {
			if(!dto.isParamValid()) {
				return Result.fail(AppResultCode.FAIL, ResultEnum.requestParamFail.getMsg());
			}
			PatientsValueObj obj = (PatientsValueObj) ShiftUtils.objToNewObj(dto, PatientsValueObj.class);
			
			//1.查询就诊列表
			UserPatientInfo userPatientInfo = patientRepository.getPatientsInfoList(obj);
			if(userPatientInfo==null || StringUtil.isEmpty(userPatientInfo.getUSER_ID())) {
				return Result.fail(ResultEnum.NotFundUser);
			}
			
			//2.删除就诊人
			DomainResult result = userPatientInfo.deletePatient(dto.getPATIENT_ID());
			if(!result.isSuccess()) {
				return Result.fail(ResultEnum.NotFoundPatient);
			}
			
			//3.保存修改信息
			patientRepository.save(userPatientInfo);
		} catch (Exception e) {
			log.info("程序异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return Result.success();
	}
	
}
